﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Orange.Lib.Effective
{
    class LamdaUse
    {
        public static void Start()
        {
            var list = new List<Tuple<int, int>>();
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    if (i + j < 10)
                    {
                        list.Add(Tuple.Create(i, j));
                    }
                }
            }

            Debug.WriteLine("get list");

            list.Sort(
                (p1, p2) =>
                (p1.Item1 * p1.Item1 + p1.Item2 * p1.Item2).CompareTo(p2.Item1 * p2.Item1 + p2.Item2 * p2.Item2)
            );

            Debug.WriteLine("get sort");

            var list2 = Enumerable.Range(0, 10)
                                         .SelectMany(x => Enumerable.Range(0, 10),
                                             (x, y) => Tuple.Create(x, y))
                                          .Where(p => p.Item1 + p.Item2 < 10)
                                          .OrderBy(p =>
                                                        p.Item1 * p.Item1 + p.Item2 * p.Item2).ToList();

            Debug.WriteLine("get sorted list2");

            Debug.WriteLine("obj.Equal(): " + list.Equals(list2));
            Debug.WriteLine("object.Equals: " + object.Equals(list, list2));
            Debug.WriteLine("object.ReferencesEqual: " + object.ReferenceEquals(list, list2));


#if DEBUG
            foreach (var p in list)
            {
                Debug.WriteLine("x:{0},y:{1},d={2}", p.Item1, p.Item2, p.Item1 * p.Item1 + p.Item2 * p.Item2);
            }
#endif

            Debug.WriteLine("Try3 is over.");
        }
    }
}
